package com.colabo.j2ee.web.oaf.security.impl;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.util.Assert;

public class CustomAuthenticationSuccessHandler
		implements AuthenticationSuccessHandler, InitializingBean {
	// Instance Fields =====================================
	
	private String welcomePage;
	
	private Log logger = LogFactory.getLog(CustomAuthenticationSuccessHandler.class);
	// Methods =============================================
	
	public void onAuthenticationSuccess(
			HttpServletRequest request,
			HttpServletResponse response,
			Authentication authentication) throws IOException,
			ServletException {
		if (logger.isDebugEnabled()) {
			logger.debug("****调用AuthenticationSuccessHandler，进行认证结束后跳转动作****");
			logger.debug("****跳转目标页面：'"+ request.getContextPath() +"/"+welcomePage + "'****");
		}
		response.sendRedirect(request.getContextPath() +"/"+welcomePage);
	}

	public void afterPropertiesSet() throws Exception {
		Assert.notNull(this.welcomePage, "welcomePage must not be null!");
	}

	public String getWelcomePage() {
		return welcomePage;
	}

	public void setWelcomePage(String welcomePage) {
		this.welcomePage = welcomePage;
	}

}
